fpath = '1-0.bin'; %'balloons.png'; 

% inputvideo = 'V3V100013_010.crop.avi'; %'video.avi';
% inputvideo = 'R:\fourier\Srinidhi\sample_videos\BaslerCapture_2011.05.10_d3_uncompressed.avi'
% inputvideo = 'R:\fourier\Srinidhi\sample_videos\V3V100004_005_512x512_uncompressed.avi'
% inputvideo = 'R:\fourier\Srinidhi\sample_videos\new_sample_videos_fromnet\New__sampled_videos_resized\1-11200part3_NEW.avi'
% inputvideo = 'R:\fourier\Srinidhi\sample_videos\new_sample_videos_fromnet\New__sampled_videos_resized\ThreePerson_Circles1_NEW.avi'
inputvideo = 'R:\fourier\Srinidhi\sample_videos\new_sample_videos_fromnet\New__sampled_videos_resized\PetsD1TeC1_part3_NEW.avi'

VISUALIZE = 1;  %0 : no visualization validate output file, 1: visualize matlab out
mov = aviread(inputvideo) ;
movinfo = aviinfo(inputvideo) ;
 
%nr_frame = size(mov(1,1).cdata, 1);
%nc_frame = size(mov(1,1).cdata, 2);
nr_frame = movinfo.Height;
nc_frame = movinfo.Width;
nr = 256; %128; %256
nc = 256; %128; %256;
NUM_FRAMES = movinfo.NumFrames; %59

% breaking frame into chips
if(nr_frame == nr && nc_frame == nc)
    cpf_r = 1;
    cpf_c = 1;
else
    cpf_r = floor(nr_frame/nr); % num of chips per frame
    cpf_c = floor(nc_frame/nc); % num of chips per frame
end
cpf = cpf_r * cpf_c;
CHIPS_PER_FRAME = cpf;

rs = 1;
cs = 1;
re = nr;
ce = nc; %256

r_start = zeros(1,cpf);
c_start = zeros(1,cpf);
r_end = zeros(1,cpf);
c_end = zeros(1,cpf);

% start and end for every chip in the frame
for j = 1:cpf_r
    for k = 1:cpf_c
        ind = (j-1)*cpf_c + k;
        c_start(ind) = cs + (k-1)*nc;
        c_end(ind) = ce + (k-1)*nc;
        r_start(ind) = rs + (j-1)*nr;
        r_end(ind) = re + (j-1)*nr;
    end
end

Nc = 3; %1; % num of center channels

% bin_dat_bar = 1;     %0 : dat file (ascii), 1 : binary file

output_nr = 16; %8; %16; % size of output saliency
output_nc = 16; %8; %16;



logfile = 'R:\fourier\MSPark\err_output_concerns\Matlab_Verification\run_RefDemo_512.log';
logfile_id = fopen(logfile,'w+');   %open the log file
qout = quantizer('ufixed', [24,20]);  %output data rep
qin = quantizer('ufixed',[8,0]); % for r, g, b values

im_curr = zeros(nr,nc,3);
im_prev = zeros(nr,nc,3);

final_salmap = zeros(16*cpf_r,16*cpf_c,NUM_FRAMES);
final_input_frame = zeros(256*cpf_r,256*cpf_c,3);
col_count = 0;
col_off   = 0;

 
  
 out_mov(1:NUM_FRAMES) = struct('cdata', [],'colormap', []);
                        
   for i = 2:NUM_FRAMES  %size(mov,2) %will go to number of frames 
 
%             if(i == 1)
%                 im_prev = zeros(nr,nc,3); %(size(mov(1,i).cdata));
%             else
%                 im_prev = frame;
%             end
%             frame = mov(1,i).cdata;
%             im_curr = frame;

        prev_chip = mov(1,i-1).cdata;
        curr_chip = mov(1,i).cdata;

     for cp = 1:CHIPS_PER_FRAME
  
        j = cp-1;
        r0 = r_start(cp);
        r1 = r_end(cp);
        c0 = c_start(cp);
        c1 = c_end(cp);
        
        im_curr = curr_chip(r0:r1,c0:c1,:);
        im_prev = prev_chip(r0:r1,c0:c1,:);
        
       %% Create dat files for the input chips
%        rgb2dat_n('3C36F-c.dat', im_curr, 2);
%        rgb2dat_n('3C36F-p.dat', im_prev, 2);

  
        [Sal, iSal, cSal, oSal, mSal, fSal] = saliency_hw_sim(im_prev, im_curr, nr, nc, Nc, i+1);
%         col_count = (cp-1) + 1;
%         if (col_count > 3)
%             row_off = 2;
%         else
%             row_off = 1;
%         end  
%         col_off = col_off + 1;
%         
%         final_salmap(16*(row_off-1)+1:16*(row_off-1)+16, 16*(col_off-1)+1:16*(col_off-1)+16,i)= Sal/5;
%        % final_input_frame(256*(row_off-1)+1:256*(row_off-1)+256, 256*(col_off-1)+1:256*(col_off-1)+256,:)= curr_chip;
%         if (col_off == 3)
%             col_off = 0;
%         end
 
%         col_count = (cp-1) + 1;
%         if (col_count > 3)
%             row_off = 2;
%         else
%             row_off = 1;
%         end  
%         col_off = col_off + 1;
        
        if(col_off == cpf_c)
            col_off = 1;
        else
            col_off = col_off + 1;
        end
        row_off = ceil(cp/cpf_c);
        
        final_salmap(16*(row_off-1)+1:16*(row_off-1)+16, 16*(col_off-1)+1:16*(col_off-1)+16,i)= Sal/5;
       % final_input_frame(256*(row_off-1)+1:256*(row_off-1)+256, 256*(col_off-1)+1:256*(col_off-1)+256,:)= curr_chip;
%         if (col_off == 3)
%             col_off = 0;
%         end

   
    minsal = min(min(Sal/5));
    maxsal_r = max(max(Sal/5));
    err_r = maxsal_r > 0.99;
    err_r = err_r * 1111;
    
     fprintf(logfile_id,'Chip %d, Frame %d -- Max255_REF = %d, Max_Ref = %f \n', j, i, err_r, maxsal_r);
     end
    
   end
   
%close the log file
 fclose(logfile_id);
 
aviobj = avifile('saliency_NEW.avi','compression','None'); 
if(VISUALIZE == 1)  %if visualization enabled
    scrsz= get(0,'ScreenSize');
%     h = figure('position',[200 scrsz(4)/3 scrsz(3)/2 scrsz(4)/3]);
    h = figure('position',[10 scrsz(4)/3 scrsz(3) scrsz(4)/3]);
       
    p0 = subplot(1,2,1);
    axis equal
    %%set(gca,'nextplot', 'replacechildren');
    p1 = subplot(1,2,2);
    axis equal    
    
%     p0 = subplot(1,2,1);
%     axis equal
%     p2 = subplot(1,2,2);
%     axis equal       

end 

 for i= 2:NUM_FRAMES
 if(VISUALIZE)
        axes(p0)
        imagesc(mov(1,i).cdata);
        title (['Input Frame','--> [ frame-id = ', num2str(i),' ]']);  

        axes(p1)
        imagesc(final_salmap(:,:,i));        %Visualize REF output
%         imshow(imresize(Sal/5, [nr,nc]), [0,1]);        %Visualize REF output
%         imshow(Sal/5, [0,1]);        %Visualize REF output
        title (['Ref Saliency','--> [ frame-id = ', num2str(i),' ]']);
        F = getframe(h);
        aviobj = addframe(aviobj,F);
        
        pause(0.01) %0.25
  %      out_mov(i)= getframe(h);
 else
     aviobj = addframe(aviobj,final_salmap(:,:,i));
 end
 end
 aviobj = close(aviobj);

 %% Make movie 
 close(h);
 %movie2avi(out_mov, 'SalRef_256.avi', 'compression', 'None');
 
        
